﻿using B.S.DataBase.MyProject.Domain.Clients;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.CientsCommand.QuearyCustomerCommands;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ClientsCommandHandler.GetClient
{
    /// <summary>
    /// 用户档案查询命令处理
    /// </summary>
    public class GetCilentHandler : IRequestHandler<QuearyClientsCommand, APIResult<List<CustomerProfiles>>>
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly IBaseRepository<CustomerProfiles> _customerProfiles;

        public GetCilentHandler(IBaseRepository<CustomerProfiles> customerProfiles)
        {
            _customerProfiles = customerProfiles;
        }

        public Task<APIResult<List<CustomerProfiles>>> Handle(QuearyClientsCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<CustomerProfiles>> result = new APIResult<List<CustomerProfiles>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            var list = _customerProfiles.GetAll().Result;
            if (!string.IsNullOrEmpty(request.CustomerName))
            {
                list = list.Where(x => x.CustomerName.Contains(request.CustomerName));
            }
            if (!string.IsNullOrEmpty(request.Contact))
            {
                list = list.Where(x => x.Contact.Contains(request.Contact));
            }
            result.data = list.ToList();

            return Task.FromResult(result);
        }
    }
}
